<?php

class Admin_Form_Curso extends Zend_Form
{

    public function init()
    {   
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $id = $front->getRequest()->getParam('id', 0);
        
        $this->setName('form_'.$controller);
        
        Zend_Loader::loadClass('dominio','library');
        
        if($id > 0){
            $obj = new Admin_Model_DbTable_Curso();
            $data = $obj->fetchRow('CUR_ID = '.(int)$id);
        }
        
        $el = new Zend_Form_Element_Hidden('CUR_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CUR_NOM');
        $el->setLabel('Nome')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CUR_ANO');
        $el->setLabel('Ano')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class','data_a');
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Radio('CUR_TPOTMP',array());
        $el->setLabel('Tipo semestre')
        ->setRequired(true)
        ->addValidator('NotEmpty')
        ->addMultiOptions(dominio::TipoSemestre());
        //->setValue($arr_set);
        $el->setAttrib('class','tpotmp');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Radio('CUR_SEM');
        $el->setLabel('Semestre')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->addMultiOptions(dominio::Semestres()); 
        //$el->setAttrib('required','');
        $this->addElement($el);
       
        
        $el = new Zend_Form_Element_Text('CUR_HORCRG');
        $el->setLabel('Carga Horária')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class', 'texto');
        //$el->setAttrib('required','');
        $this->addElement($el);
        
        /*
        $obj = new Admin_Model_DbTable_Curso();
        $row = $obj->fetchRow('CUR_ID = '.(int)$id);
        $arr_set = array();
        foreach($row as $r) $arr_set[$r['TUR_ID']] = $r['TUR_ID']; 
        */
        $el = new Zend_Form_Element_Radio('TUR_ID',array());
        $el->setLabel('Turnos')
        ->setRequired(true)
        ->addValidator('NotEmpty')
        ->addMultiOptions(dominio::Turno());
        //->setValue($arr_set);
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('CUR_TRM');
        $el->setLabel('Turma')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required','');
        $el->setAttrib('class','nummin')->setAttrib('maxLength', 3);
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CUR_NMRMAXALU');
        $el->setLabel('Número Máximo de Alunos')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('Class','nummin')->setAttrib('maxLength', 3);
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CUR_VLR');
        $el->setLabel('Valor R$')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class','valor');
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('CUR_PERDSC');
        $el->setLabel('Desconto à vista %')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class','valor');
        $el->setAttrib('required','');
        $this->addElement($el);
        
          
        $el = new Zend_Form_Element_Text('CUR_DATPRO');
        $el->setLabel('Validade da Promoção')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class','data');
        //$el->setAttrib('required','');
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Text('CUR_VLRPRO');
        $el->setLabel('Valor R$ Promocional')->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class','valor');
        //$el->setAttrib('required','');
        $this->addElement($el);
        
        $obj = new Admin_Model_DbTable_Formaspagamento();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r) $arr[$r['FOR_ID']] = $r['FOR_NOM']; 
        
        
        $obj = new Admin_Model_DbTable_Cursoforma();
        $row = $obj->fetchAll('CUR_ID = '.(int)$id);
        $arr_set = array();
        foreach($row as $r) $arr_set[$r['FOR_ID']] = $r['FOR_ID']; 
        
        $el = new Zend_Form_Element_MultiCheckbox('FOR_ID',array());
        $el->setLabel('Formas de Pagamento')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty')
        ->addMultiOptions($arr)
        ->setValue($arr_set);
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $list = array();
        if($id > 0) foreach(explode(',',$data['MTL_ID']) as $temp) $list[$temp] = $temp;
       
        $html = '<select id="MTL_ID" name="MTL_ID[]" class="chosen-select" multiple="multiple">';
        $obj = new Admin_Model_DbTable_Material();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r){ //$arr[$r['MTL_ID']] = $r['MAT_NOM']; 
            $selected = array_search($r['MTL_ID'], $list) > '' ? 'selected=""':'';            
            $html .= '<option '.$selected.' value="'.$r['MTL_ID'].'">'.$r['MTL_NOM'].'</option>'; 
        }
        $html .= '</select>';
        
        $el = new Zend_Form_Element('MTL_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Material');
        $this->addElement($el);
        
        /*
        $el = new Zend_Form_Element_Text('MTL_ID',array());
        $el->setLabel('Material')
        //->setRequired(true)
        //->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('class','autosugestao');
        $this->addElement($el);
        */
        
        $el = new Zend_Form_Element_Checkbox('CUR_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Gravar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
    }


}




